package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IdleAffiliateCpaIncomeDetailMapper;
import com.ruoyi.system.domain.IdleAffiliateCpaIncomeDetail;
import com.ruoyi.system.service.IIdleAffiliateCpaIncomeDetailService;

/**
 * 闲鱼联盟CPA收入明细Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class IdleAffiliateCpaIncomeDetailServiceImpl implements IIdleAffiliateCpaIncomeDetailService 
{
    @Autowired
    private IdleAffiliateCpaIncomeDetailMapper idleAffiliateCpaIncomeDetailMapper;

    /**
     * 查询闲鱼联盟CPA收入明细
     * 
     * @param id 闲鱼联盟CPA收入明细主键
     * @return 闲鱼联盟CPA收入明细
     */
    @Override
    public IdleAffiliateCpaIncomeDetail selectIdleAffiliateCpaIncomeDetailById(Long id)
    {
        return idleAffiliateCpaIncomeDetailMapper.selectIdleAffiliateCpaIncomeDetailById(id);
    }

    /**
     * 查询闲鱼联盟CPA收入明细列表
     * 
     * @param idleAffiliateCpaIncomeDetail 闲鱼联盟CPA收入明细
     * @return 闲鱼联盟CPA收入明细
     */
    @Override
    public List<IdleAffiliateCpaIncomeDetail> selectIdleAffiliateCpaIncomeDetailList(IdleAffiliateCpaIncomeDetail idleAffiliateCpaIncomeDetail)
    {
        // 设置查询条件，只查询待发放(1)和已发放(2)的数据
        if (idleAffiliateCpaIncomeDetail == null) {
            idleAffiliateCpaIncomeDetail = new IdleAffiliateCpaIncomeDetail();
        }
        // 设置状态为待发放或已发放
        idleAffiliateCpaIncomeDetail.setState(1); // 默认查询待发放
        List<IdleAffiliateCpaIncomeDetail> list = idleAffiliateCpaIncomeDetailMapper.selectIdleAffiliateCpaIncomeDetailList(idleAffiliateCpaIncomeDetail);
        
        // 查询已发放的数据
        idleAffiliateCpaIncomeDetail.setState(2);
        list.addAll(idleAffiliateCpaIncomeDetailMapper.selectIdleAffiliateCpaIncomeDetailList(idleAffiliateCpaIncomeDetail));
        
        return list;
    }

    /**
     * 新增闲鱼联盟CPA收入明细
     * 
     * @param idleAffiliateCpaIncomeDetail 闲鱼联盟CPA收入明细
     * @return 结果
     */
    @Override
    public int insertIdleAffiliateCpaIncomeDetail(IdleAffiliateCpaIncomeDetail idleAffiliateCpaIncomeDetail)
    {
        return idleAffiliateCpaIncomeDetailMapper.insertIdleAffiliateCpaIncomeDetail(idleAffiliateCpaIncomeDetail);
    }

    /**
     * 修改闲鱼联盟CPA收入明细
     * 
     * @param idleAffiliateCpaIncomeDetail 闲鱼联盟CPA收入明细
     * @return 结果
     */
    @Override
    public int updateIdleAffiliateCpaIncomeDetail(IdleAffiliateCpaIncomeDetail idleAffiliateCpaIncomeDetail)
    {
        return idleAffiliateCpaIncomeDetailMapper.updateIdleAffiliateCpaIncomeDetail(idleAffiliateCpaIncomeDetail);
    }

    /**
     * 批量删除闲鱼联盟CPA收入明细
     * 
     * @param ids 需要删除的闲鱼联盟CPA收入明细主键
     * @return 结果
     */
    @Override
    public int deleteIdleAffiliateCpaIncomeDetailByIds(Long[] ids)
    {
        return idleAffiliateCpaIncomeDetailMapper.deleteIdleAffiliateCpaIncomeDetailByIds(ids);
    }

    /**
     * 删除闲鱼联盟CPA收入明细信息
     * 
     * @param id 闲鱼联盟CPA收入明细主键
     * @return 结果
     */
    @Override
    public int deleteIdleAffiliateCpaIncomeDetailById(Long id)
    {
        return idleAffiliateCpaIncomeDetailMapper.deleteIdleAffiliateCpaIncomeDetailById(id);
    }

    /**
     * 根据账单ID查询闲鱼联盟CPA收入明细
     * 
     * @param billId 账单ID
     * @return 闲鱼联盟CPA收入明细
     */
    @Override
    public IdleAffiliateCpaIncomeDetail selectIdleAffiliateCpaIncomeDetailByBillId(String billId)
    {
        return idleAffiliateCpaIncomeDetailMapper.selectIdleAffiliateCpaIncomeDetailByBillId(billId);
    }

    /**
     * 根据子发布者ID查询闲鱼联盟CPA收入明细
     * 
     * @param subPublisherId 子发布者ID
     * @return 闲鱼联盟CPA收入明细
     */
    @Override
    public List<IdleAffiliateCpaIncomeDetail> selectIdleAffiliateCpaIncomeDetailBySubPublisherId(String subPublisherId)
    {
        return idleAffiliateCpaIncomeDetailMapper.selectIdleAffiliateCpaIncomeDetailBySubPublisherId(subPublisherId);
    }

    /**
     * 根据被邀请者ID查询闲鱼联盟CPA收入明细
     * 
     * @param inviteeId 被邀请者ID
     * @return 闲鱼联盟CPA收入明细
     */
    @Override
    public List<IdleAffiliateCpaIncomeDetail> selectIdleAffiliateCpaIncomeDetailByInviteeId(String inviteeId)
    {
        return idleAffiliateCpaIncomeDetailMapper.selectIdleAffiliateCpaIncomeDetailByInviteeId(inviteeId);
    }

    /**
     * 根据活动ID查询闲鱼联盟CPA收入明细
     * 
     * @param campaignId 活动ID
     * @return 闲鱼联盟CPA收入明细
     */
    @Override
    public List<IdleAffiliateCpaIncomeDetail> selectIdleAffiliateCpaIncomeDetailByCampaignId(String campaignId)
    {
        return idleAffiliateCpaIncomeDetailMapper.selectIdleAffiliateCpaIncomeDetailByCampaignId(campaignId);
    }

    /**
     * 获取CPA收入明细的统计信息
     * 
     * @return 统计信息，包含总金额、已结算金额、未结算金额和差值
     */
    @Override
    public Map<String, Object> getCpaIncomeStatistics()
    {
        Map<String, Object> result = new HashMap<>();
        
        // 获取所有记录
        List<IdleAffiliateCpaIncomeDetail> allRecords = idleAffiliateCpaIncomeDetailMapper.selectIdleAffiliateCpaIncomeDetailList(new IdleAffiliateCpaIncomeDetail());
        
        // 计算总金额
        BigDecimal totalAmount = BigDecimal.ZERO;
        // 计算已发放金额（state=2的记录）
        BigDecimal settledAmount = BigDecimal.ZERO;
        
        for (IdleAffiliateCpaIncomeDetail record : allRecords) {
            if (record.getAmount() != null) {
                totalAmount = totalAmount.add(record.getAmount());
            }
        }
        
        // 计算未发放金额
        BigDecimal unsettledAmount = totalAmount.subtract(settledAmount);
        
        result.put("totalAmount", totalAmount);
        result.put("settledAmount", settledAmount);
        result.put("unsettledAmount", unsettledAmount);
        result.put("difference", unsettledAmount); // 差值就是未发放金额
        
        return result;
    }
} 